/*     */ package com.zimbra.cs.security.sasl;
/*     */ 
/*     */ import java.io.IOException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import javax.security.sasl.SaslClient;
/*     */ import javax.security.sasl.SaslServer;
/*     */ import org.apache.mina.core.buffer.IoBuffer;
/*     */ import org.apache.mina.core.filterchain.IoFilter.NextFilter;
/*     */ import org.apache.mina.core.filterchain.IoFilterAdapter;
/*     */ import org.apache.mina.core.filterchain.IoFilterChain;
/*     */ import org.apache.mina.core.session.IoSession;
/*     */ import org.apache.mina.core.write.DefaultWriteRequest;
/*     */ import org.apache.mina.core.write.WriteRequest;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SaslFilter
/*     */   extends IoFilterAdapter
/*     */ {
/*     */   private final SaslSecurityLayer mSecurityLayer;
/*     */   private final SaslInputBuffer mInputBuffer;
/*     */   private final SaslOutputBuffer mOutputBuffer;
/*     */   private static final boolean DEBUG = false;
/*  45 */   public static final String DISABLE_ENCRYPTION_ONCE = SaslFilter.class.getName() + ".DisableEncryptionOnce";
/*     */   
/*     */   public SaslFilter(SaslServer server)
/*     */   {
/*  49 */     this(SaslSecurityLayer.getInstance(server));
/*     */   }
/*     */   
/*     */   public SaslFilter(SaslClient client) {
/*  53 */     this(SaslSecurityLayer.getInstance(client));
/*     */   }
/*     */   
/*     */   public SaslFilter(SaslSecurityLayer securityLayer) {
/*  57 */     this.mSecurityLayer = securityLayer;
/*  58 */     this.mInputBuffer = new SaslInputBuffer(securityLayer.getMaxRecvSize());
/*  59 */     this.mOutputBuffer = new SaslOutputBuffer(securityLayer.getMaxSendSize());
/*     */   }
/*     */   
/*     */   public void messageReceived(IoFilter.NextFilter nextFilter, IoSession session, Object message) throws IOException
/*     */   {
/*  64 */     IoBuffer buf = (IoBuffer)message;
/*  65 */     debug("messageReceived: size = %d", new Object[] { Integer.valueOf(buf.remaining()) });
/*  66 */     synchronized (this.mInputBuffer)
/*     */     {
/*  68 */       while (buf.hasRemaining()) {
/*  69 */         debug("messageReceived: remaining = %d", new Object[] { Integer.valueOf(buf.remaining()) });
/*  70 */         this.mInputBuffer.put(buf);
/*  71 */         debug("messageReceived: remaining = %d", new Object[] { Integer.valueOf(buf.remaining()) });
/*  72 */         debug("messageReceived: length = %d", new Object[] { Integer.valueOf(this.mInputBuffer.getLength()) });
/*  73 */         if (this.mInputBuffer.isComplete()) {
/*  74 */           debug("messageReceived: input complete", new Object[0]);
/*  75 */           byte[] b = this.mInputBuffer.unwrap(this.mSecurityLayer);
/*  76 */           nextFilter.messageReceived(session, IoBuffer.wrap(b));
/*  77 */           this.mInputBuffer.clear();
/*     */         }
/*     */       }
/*     */     }
/*  81 */     buf.clear();
/*     */   }
/*     */   
/*     */   public void filterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws IOException
/*     */   {
/*  86 */     IoBuffer buf = (IoBuffer)writeRequest.getMessage();
/*     */     
/*     */ 
/*  89 */     if (session.containsAttribute(DISABLE_ENCRYPTION_ONCE)) {
/*  90 */       debug("filterWrite: before encryption size = %d", new Object[] { Integer.valueOf(buf.remaining()) });
/*  91 */       session.removeAttribute(DISABLE_ENCRYPTION_ONCE);
/*  92 */       nextFilter.filterWrite(session, writeRequest);
/*  93 */       return;
/*     */     }
/*     */     
/*     */ 
/*  97 */     debug("filterWrite: message size = %d", new Object[] { Integer.valueOf(buf.remaining()) });
/*  98 */     if (buf.remaining() == 0)
/*     */     {
/* 100 */       debug("filterWrite: skipping encryption of empty buffer", new Object[0]);
/* 101 */       nextFilter.filterWrite(session, writeRequest);
/* 102 */       return;
/*     */     }
/*     */     
/* 105 */     List<IoBuffer> buffers = encrypt(buf);
/* 106 */     buf.clear();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 112 */     int size = buffers.size();
/* 113 */     for (int i = 0; i < size - 1; i++) {
/* 114 */       nextFilter.filterWrite(session, new DefaultWriteRequest(buffers.get(i)));
/*     */     }
/* 116 */     nextFilter.filterWrite(session, new DefaultWriteRequest(buffers.get(size - 1), writeRequest.getFuture()));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private List<IoBuffer> encrypt(IoBuffer buf)
/*     */     throws IOException
/*     */   {
/* 126 */     debug("encrypt enter: input buffer size = %d", new Object[] { Integer.valueOf(buf.remaining()) });
/* 127 */     List<IoBuffer> buffers = new ArrayList(2);
/* 128 */     synchronized (this.mOutputBuffer)
/*     */     {
/*     */       do {
/* 131 */         this.mOutputBuffer.put(buf);
/* 132 */         byte[] b = this.mOutputBuffer.wrap(this.mSecurityLayer);
/* 133 */         debug("encrypt wrap: encrypted buffer size = %d", new Object[] { Integer.valueOf(b.length) });
/* 134 */         buffers.add(IoBuffer.allocate(4).putInt(b.length).flip());
/* 135 */         buffers.add(IoBuffer.wrap(b));
/* 136 */         this.mOutputBuffer.clear();
/* 137 */       } while (buf.hasRemaining());
/*     */     }
/* 139 */     debug("encrypt exit: buffer count = %d", new Object[] { Integer.valueOf(buffers.size()) });
/* 140 */     return buffers;
/*     */   }
/*     */   
/*     */   public void onPostRemove(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter) throws IOException
/*     */   {
/* 145 */     debug("onPostRemove: enter", new Object[0]);
/* 146 */     this.mSecurityLayer.dispose();
/*     */   }
/*     */   
/*     */   private static void debug(String format, Object... args) {}
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/security/sasl/SaslFilter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */